package com.ruoyi.order.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.order.utils.IdWorker;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbOrderMapper;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;

import javax.annotation.Resource;

/**
 * 订单Service业务层处理
 *
 * @author 12692
 * @date 2023-06-26
 */
@Service
public class TbOrderServiceImpl implements ITbOrderService {
    @Resource
    private StringRedisTemplate redis;

    @Resource
    private TbOrderMapper tbOrderMapper;

    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Resource
    private IdWorker idWorker;

    /**
     * 查询订单
     *
     * @param orderId 订单主键
     * @return 订单
     */
    @Override
    public TbOrder selectTbOrderByOrderId(String orderId) {
        return tbOrderMapper.selectTbOrderByOrderId(orderId);
    }

    /**
     * 查询订单列表
     *
     * @param tbOrder 订单
     * @return 订单
     */
    @Override
    public List<TbOrder> selectTbOrderList(TbOrder tbOrder) {
        return tbOrderMapper.selectTbOrderList(tbOrder);
    }

    /**
     * 新增订单
     *
     * @param tbOrder 订单
     * @return 结果
     */
    @Override
    public int insertTbOrderOne(TbOrder tbOrder) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        tbOrder.setOrderId(uuid);
        return tbOrderMapper.insertTbOrderOne(tbOrder);
    }

    /**
     * 修改订单
     *
     * @param tbOrder 订单
     * @return 结果
     */
    @Override
    public int updateTbOrder(TbOrder tbOrder) {
        return tbOrderMapper.updateTbOrder(tbOrder);
    }

    /**
     * 批量删除订单
     *
     * @param orderIds 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderIds(String[] orderIds) {
        return tbOrderMapper.deleteTbOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单信息
     *
     * @param orderId 订单主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderId(String orderId) {
        return tbOrderMapper.deleteTbOrderByOrderId(orderId);
    }

    /**
     * 新建订单
     * 订单状态：
     * 0、订单开始
     * 1、司机接单
     * 2、司机到达乘客起点
     * 3、乘客上车，司机开始行程
     * 4、到达目的地，行程结束，未付款
     * 5、发起收款
     * 6、已支付
     * 7、订单取消
     * 10、去接乘客
     * 11、待派订单
     *
     * @param tbOrder 订单信息(用户id、起点、终点、总里程、预计时长)
     * @return
     */
    @Override
    public TbOrder createOrder(TbOrder tbOrder) {
        tbOrder.setOrderCreateTime(new Date());
        tbOrder.setOrderId(UUID.randomUUID().toString().replaceAll("-", ""));
        tbOrder.setOrderNumber(idWorker.nextId() + "");
        tbOrder.setOrderStatus((long) 11);
        System.out.println(tbOrder);
        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
            s = objectMapper.writeValueAsString(tbOrder);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(s);
        redis.opsForValue().set(tbOrder.getOrderId(), s);
        return tbOrder;
    }

    @Override
    public int getAllOrderListNumber() {
        int total = tbOrderMapper.getAllOrderListNumber();
        return total;
    }
}
